/*
 * Copyright 2011 Andy Lee.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package uk.co.optimisticpanda.remoteweb.command


import static org.junit.Assert.*
import junit.framework.TestCase

public class CommandUtilsTest extends TestCase{

	def commands = new SampleCommands().getComs()

	public void testExecute() throws Exception {
		println "\nExecute:"
		traverse(CommandUtils.execute)
	}

	public void testDisplay() throws Exception {
		println "\nExecute:"
		traverse(CommandUtils.display)
	}

	public void testTraverseParents() throws Exception {
		println "\nExecute:"
		traverse(CommandUtils.containers)
	}

	def traverse(c){
		CommandUtils.traverse(commands, c)
	}

	public void testFindParentsDefaultArg() throws Exception {
		def parents = CommandUtils.findAllParents(commands).collect { it.description }
		assertEquals ( parents, [
			"parent1",
			"parent2",
			"parent3"
		])
	}

	public void testFindParents() throws Exception {
		def parents = CommandUtils.findAllParents(commands, 0).collect { it.description }
		assertEquals ( parents, [
			"parent1",
			"parent2",
			"parent3"
		])
	}

	public void testFindParentsLevel1() throws Exception {
		def parents = CommandUtils.findAllParents(commands, 1).collect { it.description }
		assertEquals ( parents, [
			"coms2subParent1",
			"coms3subParent1"
		])
	}

	public void testFindParentsLevel2() throws Exception {
		def parents = CommandUtils.findAllParents(commands, 2).collect { it.description }
		assertEquals ( parents, ["coms5Parent"])
	}

	public void testFindParentsLevel3() throws Exception {
		def parents = CommandUtils.findAllParents(commands, 3).collect { it.description }
		assertEquals ( parents, [])
	}

	public void testFindAllLeavesDefault() throws Exception {
		def parents = CommandUtils.findAllLeaves(commands).collect { it.description }
		assertEquals ( parents, ["task1", "task2"])
	}

	public void testFindAllLeavesLevel0() throws Exception {
		def parents = CommandUtils.findAllLeaves(commands, 0).collect { it.description }
		assertEquals ( parents, ["task1", "task2"])
	}

	public void testFindAllLeavesLevel1() throws Exception {
		def parents = CommandUtils.findAllLeaves(commands, 1).collect { it.description }
		assertEquals ( parents, [
			"coms2subTask1",
			"coms2subTask3",
			"coms3subTask1",
			"coms3subTask3",
			"coms6Task1",
			"coms6Task2",
			"coms6Task3"
		])
	}

	public void testFindAllLeavesLevel2() throws Exception {
		def parents = CommandUtils.findAllLeaves(commands, 2).collect { it.description }
		assertEquals ( parents, [
			"subSubTask1",
			"subSubTask2",
			"subSubTask3",
			"coms5Task1",
			"coms5Task2"
		])
	}

	public void testFindAllLeavesLevel3() throws Exception {
		def parents = CommandUtils.findAllLeaves(commands, 3).collect { it.description }
		assertEquals ( parents, [
			"coms6Task1",
			"coms6Task2",
			"coms6Task3"
		])
	}

	public void testFindAllLeavesLevel4() throws Exception {
		def parents = CommandUtils.findAllLeaves(commands, 4).collect { it.description }
		assertEquals( parents , [])
	}
}